var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    istokenPop: false,
    isnumPop: false,
    hobbys: []
  },

  goHome () {
    wx.switchTab({
      url: '/pages/newhomeindex/newhomeindex'
    })
  },


  getList2 () {
    var _this = this


    var params = {
      page:1,
      size:9999
    }

    wx.showLoading({
      title: '分类加载中',
      icon: 'none'
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/FirstPage/findAllHobbys',
      method: 'POST',
      data:params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          wx.hideLoading()

          res.data.data.forEach(function (item, index) {
            for (var i = 0; i < item.hobbyTypes.length; i++) {
          
      
                  item.hobbyTypes[i].ischoose = false
                
              
            }
            _this.setData({
              list: res.data.data
            })
          })

        }
      }
    })  
  },


  getList (array) {
    var _this = this

    wx.showLoading({
      title: '分类加载中',
      icon: 'none'
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/FirstPage/findAllHobbys',
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success:(res) =>{
        console.log(res)
        if (res.data.code == 200) {
          wx.hideLoading()

          res.data.data.forEach(function (item, index) {
            for (var i = 0; i < item.hobbyTypes.length;i++) {
              for (var j = 0; j< array.length;j++) {
                if (array[j] == item.hobbyTypes[i].hobbyTypeId) {
                  item.hobbyTypes[i].ischoose = true
                }
              }
            }
            _this.setData({
              list:res.data.data
            })
          })
          
        }
      }
    })  


  },


  //选择标签

  cutType (e) {
    var _this = this
    console.log(e)
    var hobbytypeid = e.currentTarget.dataset.hobbytypeid

    var hobbys = this.data.hobbys

    this.data.list.forEach(function (item, index) {

      for (var i = 0; i < item.hobbyTypes.length;i++) {
        if (hobbytypeid == item.hobbyTypes[i].hobbyTypeId) {
          var ischoose = "list[" + index + "].hobbyTypes[" + i + "].ischoose"
        
          _this.setData({
            [ischoose]: false
          })
          
        }
      }

      for (var i = 0; i< hobbys.length;i++) {
        if (hobbytypeid==hobbys[i]) {
          hobbys.splice(i,1)
        }
      }

      _this.setData({
        hobbys: hobbys
      })
    })
    
  },

  chooseType (e) {


    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    var _this = this
    console.log(e)
    var hobbytypeid = e.currentTarget.dataset.hobbytypeid

    var hobbys = this.data.hobbys

    this.data.list.forEach(function (item, index) {

      for (var i = 0; i < item.hobbyTypes.length; i++) {
        if (hobbytypeid == item.hobbyTypes[i].hobbyTypeId) {
          
          var ischoose = "list[" + index + "].hobbyTypes[" + i + "].ischoose"
          hobbys.push(hobbytypeid)
          _this.setData({
            [ischoose]: true,
            hobbys:hobbys
          })

        }
      }
    })
  },


  loginCode() {
    this.setData({
      istokenPop: false
      // isnumPop: true
    })
    this.onLoad()
    this.onShow()
    // setTimeout(function () {
    //   var pages = getCurrentPages(); // 当前页面
    //   var beforePage = pages[pages.length - 2]; // 前一个页面
    //   beforePage.reloadHome()

    //   wx.switchTab({
    //     url: '/pages/index/index'
    //   })
    // }, 2000)
  },

  loginNum () {
    this.setData({
      isnumPop: false
    })
    


    // setTimeout(function () {
    //   var pages = getCurrentPages(); // 当前页面
    //   var beforePage = pages[pages.length - 2]; // 前一个页面
    //   beforePage.reloadHome()

    //   wx.switchTab({
    //     url: '/pages/index/index'
    //   })
    // }, 2000)

  },


  sureGetho () {
    console.log(this.data.hobbys)

    // if (wx.getStorageSync('userTel') == '') {
    //   this.setData({
    //     isnumPop: true
    //   })
    //   return
    // }

    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }

    var hobbys = this.data.hobbys.join(',')

    this.getChangeHbbys(hobbys)
  },




  //查询用户绑定的兴趣爱好
  
  getHbbys () {
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/FirstPage/findHobbysByUserId/auth',
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) =>{
        if (res.data.code == 200) { 
          _this.setData({
            hobbys:res.data.data == "" ? []:res.data.data
          })  
          _this.getList(res.data.data == "" ?[]:res.data.data)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:"none"
          })
        }
      }
    })
  },

  getChangeHbbys(hobbys) {
    wx.showLoading({
      title: '确认选择中',
      icon: 'none'
    })
    var params = {
      hobbys: hobbys
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/FirstPage/updateUserHobbys/auth',
      method: 'POST',
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) =>{
        if (res.data.code == 200) {
          wx.hideLoading()
          wx.showToast({
            title: '确认成功',
            icon: 'none',
            duration:2000
          })
          setTimeout(function (){
            var pages = getCurrentPages(); // 当前页面
            var beforePage = pages[pages.length - 2]; // 前一个页面
            beforePage.reloadHome()

            // wx.navigateBack()
            
            wx.switchTab({
              url: '/pages/index/index'
            })
          },2000)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
      
    })
  },


  // loginCode() {
  //   this.setData({
  //     istokenPop: false
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getUserset()
    if  (wx.getStorageSync('token')!='')  {
      this.getHbbys()
    }else{
      this.getList2()
    }

  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})